<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
    <title>fast-mpg</title>
    <link rel="stylesheet"
          th:href="@{/bootstrap-3.4.1/css/bootstrap.min.css}">

    <link rel="stylesheet"
          th:href="@{/bootstrapValidator.min.css}">
    <link rel="stylesheet"
          th:href="@{/table/bootstrap-table.min.css}">

    <link rel="stylesheet"
          th:href="@{/bootstrap-3.4.1/css/bootstrap-theme.min.css}">

    <script th:src="@{/jquery-3.6.0.min.js}"></script>
    <script th:src="@{/bootstrap-3.4.1/js/bootstrap.min.js}"></script>
    <script th:src="@{/bootstrapValidator.min.js}"></script>
    <script th:src="@{/table/bootstrap-table.min.js}"></script>

    <script th:src="@{/base.js}"></script>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <nav class="navbar navbar-default navbar-static-top">
                <div class="container-fluid">
                    <!-- Brand and toggle get grouped for better mobile display -->
                    <div class="navbar-header">
                        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                            <span class="sr-only">Toggle navigation</span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                            <span class="icon-bar"></span>
                        </button>
                        <div class="navbar-brand bg-success">Fastmpg</div>
                    </div>

                    <!-- Collect the nav links, forms, and other content for toggling -->
                    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                        <ul class="nav navbar-nav">
                            <li class="active"><a href="/template">生成 </a></li>
                            <li class=""><a href="/dbsource">数据源</a></li>
                        </ul>
                        <ul class="nav navbar-nav navbar-right">
                            <li class="text-primary"><a href="javascript:void(0);" onclick="sysAbout()">关于</a></li>
                        </ul>
                    </div><!-- /.navbar-collapse -->
                </div><!-- /.container-fluid -->
            </nav>
        </div>
    </div>
    <div class="row">&nbsp;</div>
    <div class="row">&nbsp;</div>
    <div class="row">
        <div class="col-md-2">
            <div class="panel panel-default">
                <button type="button" class="btn btn-default btn-block" onClick="addTemplate()">
                    <span class="glyphicon glyphicon-plus"></span>
                    新增
                </button>
            </div>
            <div class="panel panel-default">
                <div class="list-group">
                    <a th:each="tps,stat:${templates}" th:text="${tps.name}" href="javascript:void(0);" th:onclick="|tpsEdit(${tps.id}, this)|" class="list-group-item name-label"></a>
<!--                    <a href="#" class="list-group-item active">织音</a>-->
<!--                    <a href="#" class="list-group-item">游缘</a>-->
<!--                    <a href="#" class="list-group-item">支付宝</a>-->
<!--                    <a href="#" class="list-group-item">微信支付</a>-->
                </div>
            </div>
        </div>
        <div class="col-md-10">
            <div class="panel panel-default">
                <div class="panel-heading clearfix">
                    <!-- <h6 class="pull-left">操作</h6> -->
                    <div class="pull-right clearfix">
                        <button type="button" class="btn btn-success" onClick="genTemplate()">
                            <span class="glyphicon glyphicon-play"></span>&nbsp;生成
                        </button>
                        <button type="button" class="btn btn-primary" onClick="subTemplate()" >
                            <span class="glyphicon glyphicon-floppy-disk"></span>&nbsp;保存
                        </button>
                        <button type="button" class="btn btn-danger" onClick="tpsDel()">
                            <span class="glyphicon glyphicon-trash"></span>&nbsp;删除
                        </button>
                    </div>
                </div>
                <div class="panel-body">
                    <form method="post" id="tps_form">
                        <input type="hidden" name="id" id="tps_id" >
                        <div class="row">
                            <div class="col-xs-2"></div>
                            <div class="col-xs-2">
                                <label class="control-label" for="name">名称</label>
                                <input type="text" class="form-control" placeholder="" name="name" id="name">
                            </div>
                            <div class="form-group col-xs-4">
                                <label class="control-label" for="dsId">数据源</label>
                                <select class="form-control" id="dsId" name="dsId"
                                        data-bv-notempty="true"
                                        data-bv-notempty-message="不能为空">
                                    <option th:each="ds,stat:${dslist}" th:value="${ds.id}" th:text="${ds.alias}"></option>
<!--                                    <option value="1">织音开发</option>-->
<!--                                    <option selected>织音开发</option>-->
<!--                                    <option>3</option>-->
<!--                                    <option>4</option>-->
<!--                                    <option>5</option>-->
                                </select>
                            </div>
                        </div>

                        <div class="row">&nbsp;</div>

                        <div class="row">
                            <div class="col-xs-2"></div>
                            <div class="col-xs-2">
                                <label class="control-label" for="author">作者</label>
                                <input type="text" class="form-control" placeholder="" name="author" id="author">
                            </div>
                            <div class="form-group col-xs-2">
                                <label class="control-label" for="moduleName">模块</label>
                                <input type="text" class="form-control" placeholder="" name="moduleName" id="moduleName"
                                       data-bv-notempty="true"
                                       data-bv-notempty-message="不能为空">
                            </div>
                            <div class="col-xs-2">
                                <label class="control-label" for="tablePrefix">前缀过滤</label>
                                <input type="text" class="form-control" placeholder="多个用英文逗号分隔" name="tablePrefix" id="tablePrefix">
                            </div>
                        </div>

                        <div class="row">&nbsp;</div>

                        <div class="row">
                            <div class="col-xs-2"></div>
                            <div class="form-group col-xs-6">
                                <label class="control-label" for="parent">包名</label>
                                <input type="text" class="form-control" placeholder="" name="parent" id="parent"
                                       data-bv-notempty="true"
                                       data-bv-notempty-message="不能为空">
                            </div>
                        </div>

                        <div class="row">&nbsp;</div>

                        <div class="row">
                            <div class="col-xs-2"></div>
                            <div class="form-group col-xs-6">
                                <label class="control-label" for="outputDir">输出目录</label>
                                <input type="text" class="form-control" placeholder="" name="outputDir" id="outputDir"
                                       data-bv-notempty="true"
                                       data-bv-notempty-message="不能为空">
                            </div>
                        </div>

                        <div class="row">&nbsp;</div>

                        <div class="row">
                            <div class="col-xs-2"></div>
                            <div class="col-xs-8">
                                <div class="form-group">
                                    <label for="includes">表</label>
                                    <textarea name="includes" id="includes" class="form-control" rows="3" placeholder="输入需要生成的表,多个用英文逗号(,)分隔"
                                              data-bv-notempty="true"
                                              data-bv-notempty-message="不能为空"></textarea>
                                </div>

                            </div>
                        </div>

                        <div class="row">&nbsp;</div>

                        <div class="row">
                            <div class="col-xs-2"></div>
                            <div class="form-group col-xs-2">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" id="fileOverride" name="fileOverride" value="true" >
                                        覆盖
                                    </label>
                                </div>
                            </div>
                            <div class="form-group col-xs-2">
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" id="enableSwagger" name="enableSwagger" value="true">
                                        使用Swagger
                                    </label>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
    <div class="col-md-1">

    </div>
</div>
</div>
</body>
<script th:inline="javascript">

    var dsList = [[${dslist}]];
    var curTpsId = null;
    var curItem = null;

    function subTemplate() {
        var form = $('#tps_form');
        form.bootstrapValidator('validate');
        if (!form.data('bootstrapValidator').isValid()) {
            return false;
        }
        var data = formToJson(form.serializeArray());
        $.ajax({
            type: "POST",
            url: "/main/template/add",
            contentType: "application/json; charset=utf-8",
            data: data,
            async: false,
            success: function (result) {
                alert(result);
                var valid = form.data('bootstrapValidator');
                if (valid) {
                    valid.destroy();
                }
                let dataJson = JSON.parse(data);
                if (!dataJson.id) {
                    location.reload(false);
                }
                if (curItem != null) {
                    $(curItem).text(dataJson.name);
                }
            },
            error : function() {
                alert("异常！");
            }
        });
    }

    function addTemplate() {
        curTpsId = null;
        $(".name-label").removeClass("active");
        var form = $('#tps_form');
        var valid = form.data('bootstrapValidator');
        if (valid) {
            valid.destroy();
        }
        form[0].reset();
        $("#tps_id").val("");
    }

    function tpsEdit(id, obj) {
        curTpsId = id;
        curItem = obj;
        var form = $('#tps_form');
        var valid = form.data('bootstrapValidator');
        if (valid) {
            valid.destroy();
        }
        form[0].reset();
        $(".name-label").removeClass("active");
        $(obj).addClass("active");
        $.ajax({
            type: "GET",
            url: "/main/template/get/" + id,
            dataType: "json",
            data: null,
            success: function (result) {
                $("#tps_id").val(result.id);
                $("#dsId").val(result.dsId);
                $("#name").val(result.name);
                $("#author").val(result.author);
                $("#outputDir").val(result.outputDir);
                $("#parent").val(result.parent);
                $("#moduleName").val(result.moduleName);
                $("#includes").val(result.includes);
                $("#tablePrefix").val(result.tablePrefix);
                if(result.enableSwagger) {
                    $("#enableSwagger").prop("checked", true);
                }
                if(result.fileOverride) {
                    $("#fileOverride").prop("checked", true);
                }
            }
        });
    }


    function tpsDel() {
        if(!curTpsId) {
            alert("未选中任何模板!");
            return false;
        }
        var flag = confirm("确定要删除吗?");
        if (flag) {
            $.ajax({
                type: "POST",
                url: "/main/template/del/" + curTpsId,
                success: function (result) {
                    alert(result);
                    location.reload(false);
                }
            });
        }
    }

    function genTemplate() {
        var form = $('#tps_form');
        form.bootstrapValidator('validate');
        if (!form.data('bootstrapValidator').isValid()) {
            return false;
        }
        var data = formToJson(form.serializeArray());
        $.ajax({
            type: "POST",
            url: "/main/template/gen",
            contentType: "application/json; charset=utf-8",
            data: data,
            async: false,
            success: function (result) {
                alert(result);
                form.data('bootstrapValidator').destroy();
            },
            error : function() {
                alert("异常！");
            }
        });
    }

</script>
</html>